package com.soft.base.spring.util;


import com.alibaba.fastjson.JSON;
import org.springframework.http.MediaType;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.HandlerMapping;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * class
 *
 * @author user
 * @date 2020/06/05
 */
public class Response {


    public static String json(int code, String msg) {
        return json(code, msg, null, null);
    }

    public static String json(int code, String msg, Object data) {
        return json(code, msg, data, null);
    }

    public static String json(int code, String msg, Object data, String redirect) {
        Map<String, Object> map = new HashMap<String, Object>(128);
        map.put("code", code);
        map.put("msg", msg);
        map.put("data", data);
        map.put("redirect", redirect);
        return JSON.toJSONString(map);
    }


    public static HttpServletResponse response() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
    }

    public static void setHeader(String name, String value) {
        HttpServletResponse response = response();
        if (null == response) {
            return;
        }
        response.setHeader(name, value);
        ;
    }

}
